home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 15 / vi101.zip / VI.C < prev    next >
C/C++ Source or Header  |  1990-11-07  |  27KB  |  723 lines

  1. /*----------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*  Program Name: View Icon                                                   */
  4. /*                                                                            */
  5. /*  Module name : vi.c                                                        */
  6. /*  Date        : 02-23-1989                                                  */
  7. /*  Written by  : Bob Novell                                                  */
  8. /*                                                                            */
  9. /*----------------------------------------------------------------------------*/
  10. #define _WINDOWS
  11. #include "vi.h"
  12. #include <io.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15.  
  16.  
  17. FARPROC lpfnAppDlgProc;
  18.  
  19. HANDLE hFont;
  20. HANDLE hInst;
  21. HWND   ghWnd;
  22. HWND   hWndList;
  23.  
  24. BOOL  FirstMinMax    = TRUE;
  25. BOOL  WindowActive   = FALSE;
  26. BOOL  FirstActivate  = TRUE;
  27.  
  28. char  szAppName[]    = "vi";
  29. char  szAppTitle[]   = "View Icon";
  30.  
  31. char  Directory[80]  = "c:";
  32. char  TempStr  [80];
  33.  
  34. char  ProgInit [80];                   /* path to program .ini file           */
  35.  
  36. int   CharWidth;
  37. int   CharHeight;
  38.  
  39. int   xSpacing;
  40. int   ySpacing;
  41. int   xMaxIcons;
  42.  
  43. int   yMaxIcons;
  44. int   IconsInWindow;
  45.  
  46. int   FileCount;
  47. int   CurrentIndex;
  48.  
  49. RECT  gClientRect;
  50.  
  51. KeyStruct key2scroll [] =
  52.      {
  53.      VK_HOME,  WM_VSCROLL, SB_TOP,
  54.      VK_END,   WM_VSCROLL, SB_BOTTOM,
  55.      VK_PRIOR, WM_VSCROLL, SB_PAGEUP,
  56.      VK_NEXT,  WM_VSCROLL, SB_PAGEDOWN,
  57.      VK_UP,    WM_VSCROLL, SB_LINEUP,
  58.      VK_DOWN,  WM_VSCROLL, SB_LINEDOWN,
  59.      };
  60.  
  61. #define NUMKEYS (sizeof key2scroll / sizeof key2scroll[0])
  62.  
  63. /*----------------------------------------------------------------------------*/
  64. /* FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)                              */
  65. /*                                                                            */
  66. /* PURPOSE:  Calls initialization function, processes message loop            */
  67. /*----------------------------------------------------------------------------*/
  68. int PASCAL WinMain(HANDLE Inst, HANDLE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  69.     {
  70.     MSG  msg;
  71.  
  72.     if (!hPrevInst)
  73.         {
  74.         if(!AppInit(Inst))
  75.             return(FALSE);
  76.         }
  77.     else
  78.         return(FALSE);
  79.  
  80.     ghWnd = CreateWindow(szAppName,szAppTitle,
  81.        WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_VSCROLL,
  82.        CW_USEDEFAULT,                  /* x position                          */
  83.        0,                              /* y position                          */
  84.        CW_USEDEFAULT,                  /* width                               */
  85.        0,                              /* height                              */
  86.        NULL,                           /* parent handle                       */
  87.        NULL,                           /* menu or child ID                    */
  88.        Inst,                           /* instance                            */
  89.        NULL);                          /* additional info                     */
  90.  
  91.     if (!ghWnd)
  92.         return (FALSE);
  93.  
  94.     ShowWindow(ghWnd, nCmdShow);
  95.     UpdateWindow(ghWnd);
  96.  
  97.     while (GetMessage(&msg, NULL, NULL, NULL))
  98.         {
  99.         TranslateMessage(&msg);
  100.         DispatchMessage(&msg);
  101.         }
  102.  
  103.     return (msg.wParam);
  104.     }
  105.  
  106. /*----------------------------------------------------------------------------*/
  107. /* FUNCTION: AppInit(HANDLE)                                                  */
  108. /*                                                                            */
  109. /* PURPOSE: Initializes window data and registers window class                */
  110. /*----------------------------------------------------------------------------*/
  111. BOOL AppInit(HANDLE hInstance)
  112.     {
  113.     HDC hDC;
  114.     WNDCLASS   rClass;
  115.     TEXTMETRIC tm;
  116.  
  117.     hInst = hInstance;
  118.  
  119.     hDC = GetDC(ghWnd);                /* get char width and height           */
  120.     GetTextMetrics(hDC,&tm);
  121.     CharWidth  = tm.tmAveCharWidth;
  122.     CharHeight = tm.tmHeight + tm.tmExternalLeading;
  123.     xSpacing   = (CharWidth  * 9);
  124.     ySpacing   = (CharHeight + 49);
  125.     ReleaseDC (ghWnd,hDC);
  126.  
  127.     lpfnAppDlgProc = MakeProcInstance(AppDlgProc,hInst);
  128.  
  129.     rClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  130.     rClass.hIcon         = LoadIcon(hInstance, "ViIcon");
  131.     rClass.lpszMenuName  = "AppMenu";
  132.     rClass.lpszClassName = szAppName;
  133.     rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  134.     rClass.hInstance     = hInstance;
  135.     rClass.style         = NULL;
  136.     rClass.lpfnWndProc   = AppWndProc;
  137.  
  138.     return RegisterClass(&rClass);
  139.     }
  140.  
  141. /*----------------------------------------------------------------------------*/
  142. /* FUNCTION: AppWndProc(HWND, unsigned, WORD, LONG)                           */
  143. /*                                                                            */
  144. /* PURPOSE:  Processes messages for main window                               */
  145. /*----------------------------------------------------------------------------*/
  146. long FAR PASCAL AppWndProc(HWND hWnd, uint message,WORD wParam, LONG lParam)
  147.     {
  148.     PAINTSTRUCT ps;
  149.     FARPROC     lpProcAbout;
  150.     HMENU       hMenu;
  151.  
  152.     switch (message)
  153.         {
  154.         case WM_GETMINMAXINFO:         /* ----------------------------------- */
  155.  
  156.             if(FirstMinMax)
  157.                 {                      // Set maximum size for opening window
  158.                 ((LPPOINT)lParam+4)->x = (xSpacing * 6)+20;
  159.                 ((LPPOINT)lParam+4)->y = (ySpacing * 4);
  160.                 FirstMinMax = FALSE;
  161.                 }
  162.             else                       // ensure enough room for at least 1 icon
  163.                 {
  164.                 ((LPPOINT)lParam+3)->x = (xSpacing)+20;
  165.                 ((LPPOINT)lParam+3)->y = (ySpacing * 2);
  166.                 }
  167.             break;
  168.  
  169.         case WM_SYSCOMMAND:            /* ----------------------------------- */
  170.             if (wParam == IDM_ABOUT)
  171.                 {
  172.                 lpProcAbout = MakeProcInstance(AppAbout, hInst);
  173.                 DialogBox(hInst, "vi_about", hWnd, lpProcAbout);
  174.                 FreeProcInstance(lpProcAbout);
  175.                 break;
  176.                 }
  177.             else
  178.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  179.  
  180.         case WM_SYSCOLORCHANGE:        /* ----------------------------------- */
  181.             InvalidateRect(ghWnd,NULL,TRUE); // force repaint if colors change
  182.             break;
  183.  
  184.         case WM_VSCROLL:               /* ----------------------------------- */
  185.             VerticalScrollProcess(wParam, lParam);
  186.             break;
  187.  
  188.         case WM_SIZE:                  /* ----------------------------------- */
  189.             if(WindowActive && wParam != SIZEICONIC)
  190.                 {
  191.                 int xTmp, yTmp;
  192.  
  193.                 GetClientRect (ghWnd,&gClientRect);
  194.                 xTmp = (4+ gClientRect.right  - gClientRect.left) / xSpacing;
  195.                 yTmp = (   gClientRect.bottom - gClientRect.top)  / ySpacing;
  196.  
  197.                 gClientRect.bottom = gClientRect.top + (ySpacing * yTmp);
  198.  
  199.                 if(xTmp != xMaxIcons || yTmp != yMaxIcons)
  200.                     {
  201.                     xMaxIcons = xTmp;
  202.                     yMaxIcons = yTmp;
  203.                     InvalidateRect(ghWnd,NULL,TRUE);/* force repaint          */
  204.                     IconsInWindow = xTmp * yTmp;
  205.                     if(CurrentIndex > 0)            /* force index to boundry */
  206.                         CurrentIndex -= (CurrentIndex % IconsInWindow);
  207.                     AdjustScrollBar();
  208.                     }
  209.                 }
  210.             break;
  211.  
  212.         case WM_PAINT:                 /* ----------------------------------- */
  213.             ViewIcons(BeginPaint(hWnd,&ps));
  214.             EndPaint(hWnd, &ps);
  215.             break;
  216.  
  217.         case WM_COMMAND:               /* ----------------------------------- */
  218.             AppCommand(wParam);
  219.             break;
  220.  
  221.         case WM_USER:                  /* ----------------------------------- */
  222.             StartApplication();
  223.             break;
  224.  
  225.         case WM_CREATE:                /* ----------------------------------- */
  226.             hMenu = GetSystemMenu(hWnd, FALSE);
  227.             ChangeMenu(hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  228.             ChangeMenu(hMenu, NULL, "A&bout vi...", IDM_ABOUT,
  229.                        MF_APPEND | MF_STRING);
  230.  
  231.             hWndList = CreateWindow("listbox", NULL,
  232.                                     WS_CHILD | LBS_SORT,
  233.                                     CharWidth,       CharHeight,
  234.                                     CharWidth  * 16, CharHeight * 20,
  235.                                     hWnd,  IDC_INVISIBLE, hInst, NULL);
  236.             SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
  237.             break;
  238.  
  239.         case WM_ACTIVATE:              /* ----------------------------------- */
  240.             if(wParam)
  241.                 {
  242.                 SetFocus(hWnd);
  243.                 WindowActive = TRUE;
  244.                 if(FirstActivate && !HIWORD(lParam))
  245.                     {                   // if 1st activate and not iconic
  246.                     FirstActivate = FALSE;
  247.                     PostMessage(hWnd,WM_USER,NULL,(LONG)NULL);
  248.                     }
  249.                 }
  250.             else
  251.                 WindowActive = FALSE;
  252.             break;
  253.  
  254.         case WM_KEYDOWN:
  255.             {
  256.             int i;
  257.  
  258.             for (i = 0 ; i < NUMKEYS ; i++)
  259.                  if (wParam == key2scroll[i].wVirtKey)
  260.                       {
  261.                       SendMessage (hWnd, key2scroll[i].iMessage,
  262.                            key2scroll[i].wRequest, 0L) ;
  263.                       break ;
  264.                       }
  265.             }
  266.             break;
  267.  
  268.         case WM_DESTROY:               /* ----------------------------------- */
  269.             FreeProcInstance(lpfnAppDlgProc);
  270.             PostQuitMessage(0);
  271.             break;
  272.  
  273.         default:                       /* ----------------------------------- */
  274.             return (DefWindowProc(hWnd, message, wParam, lParam));
  275.         } /* end switch */
  276.     return (NULL);
  277.     }
  278.  
  279. /*----------------------------------------------------------------------------*/
  280. /* FUNCTION: AppCommand(Word wParam)                                          */
  281. /*                                                                            */
  282. /* PURPOSE:  Processes WM_COMMAND messages from main window                   */
  283. /*----------------------------------------------------------------------------*/
  284. void AppCommand(WORD wParam)
  285.     {
  286.     switch(wParam)
  287.         {
  288.         case IDC_INVISIBLE:
  289.             break;
  290.  
  291.         case IDM_FILE:
  292.             DialogBox(hInst,"SELECT",ghWnd,lpfnAppDlgProc);
  293.             break;
  294.         }                              /* end switch                          */
  295.     }
  296.  
  297. /*----------------------------------------------------------------------------*/
  298. /* FUNCTION: VerticalScrollProcess(WORD wParam, LONG lParam)                  */
  299. /*                                                                            */
  300. /* PURPOSE:  Processes WM_VSCROLL messages from main window                   */
  301. /*----------------------------------------------------------------------------*/
  302. void VerticalScrollProcess(WORD wParam, LONG lParam)
  303.     {
  304.     HDC hDC;
  305.  
  306.     switch(wParam)
  307.         {
  308.         case SB_THUMBPOSITION:
  309.             CurrentIndex = LOWORD(lParam);
  310.             if(CurrentIndex > 0)                    /* force index to boundry */
  311.                 CurrentIndex -= (CurrentIndex % IconsInWindow);
  312.             InvalidateRect(ghWnd, NULL, TRUE);
  313.             break;
  314.  
  315.         case SB_TOP:
  316.             if(CurrentIndex > 0)
  317.                 {
  318.                 CurrentIndex = 0;
  319.                 InvalidateRect(ghWnd, NULL, TRUE);
  320.                 }
  321.             break;
  322.  
  323.         case SB_BOTTOM:
  324.             if(CurrentIndex + IconsInWindow < FileCount)
  325.                 {
  326.                 CurrentIndex = FileCount - IconsInWindow;
  327.                 InvalidateRect(ghWnd, NULL, TRUE);
  328.                 }
  329.             break;
  330.  
  331.         case SB_LINEUP:
  332.             if(CurrentIndex > 0)
  333.                 {
  334.                 CurrentIndex -= xMaxIcons;
  335.                 ScrollWindow(ghWnd, 0, ySpacing,
  336.                   (LPRECT)&gClientRect, (LPRECT)&gClientRect);
  337.                 ValidateRect(ghWnd, NULL);
  338.                 hDC = GetDC(ghWnd);
  339.                 ViewIconRow(hDC, 0);
  340.                 ReleaseDC(ghWnd, hDC);
  341.                 }
  342.             break;
  343.  
  344.         case SB_LINEDOWN:
  345.             if((CurrentIndex + IconsInWindow) < FileCount)
  346.                 {
  347.                 CurrentIndex += xMaxIcons;
  348.                 ScrollWindow(ghWnd, 0, -ySpacing,
  349.                   (LPRECT)&gClientRect, (LPRECT)&gClientRect);
  350.                 ValidateRect(ghWnd, NULL);
  351.                 hDC = GetDC(ghWnd);
  352.                 ViewIconRow(hDC, yMaxIcons-1);
  353.                 ReleaseDC(ghWnd, hDC);
  354.                 }
  355.             break;
  356.  
  357.         case SB_PAGEUP:
  358.             if(CurrentIndex > 0)
  359.                 {
  360.                 if(CurrentIndex >= IconsInWindow)
  361.                     CurrentIndex -= IconsInWindow;
  362.                 else
  363.                     CurrentIndex = 0;
  364.                 InvalidateRect(ghWnd, NULL, TRUE);
  365.                 }
  366.             break;
  367.  
  368.         case SB_PAGEDOWN:
  369.             if((CurrentIndex + IconsInWindow) < FileCount)
  370.                 {
  371.                 CurrentIndex += IconsInWindow;
  372.                 InvalidateRect(ghWnd, NULL, TRUE);
  373.                 }
  374.             break;
  375.  
  376.         default:
  377.             break;
  378.         }
  379.     SetScrollPos(ghWnd, SB_VERT, (CurrentIndex > IconsInWindow ?
  380.       CurrentIndex+IconsInWindow : CurrentIndex), TRUE);
  381.     }
  382.  
  383. /*----------------------------------------------------------------------------*/
  384. /* FUNCTION: AppAbout(HWND, uint, WORD, LONG)                                 */
  385. /*                                                                            */
  386. /* PURPOSE:  Processes messages for "About" dialog box                        */
  387. /*----------------------------------------------------------------------------*/
  388. BOOL FAR PASCAL AppAbout(HWND hDlg, uint message,WORD wParam, LONG lParam)
  389.     {
  390.     switch (message)
  391.         {
  392.         case WM_INITDIALOG:
  393.             return (TRUE);
  394.  
  395.         case WM_COMMAND:
  396.             if (wParam == IDOK)
  397.                 {
  398.                 EndDialog(hDlg, TRUE);
  399.                 return (TRUE);
  400.                 }
  401.             break;
  402.         }
  403.     return (FALSE);
  404.     }
  405.  
  406. /*----------------------------------------------------------------------------*/
  407. /* FUNCTION: AppDlgProc(HWND, unsigned, WORD, LONG)                           */
  408. /*                                                                            */
  409. /* PURPOSE:  Processes Dialog Box Messages                                    */
  410. /*----------------------------------------------------------------------------*/
  411. BOOL FAR PASCAL AppDlgProc(HWND hDlg, uint message, WORD wParam, LONG lParam)
  412.     {
  413.     static BOOL ListBoxFocus = FALSE;
  414.  
  415.     switch(message)
  416.         {
  417.         case WM_INITDIALOG:
  418.             strcpy(TempStr, Directory);
  419.             UpdateListBox(hDlg);
  420.             SetFocus(GetDlgItem(hDlg, IDC_LISTBOX));
  421.             return FALSE;
  422.             break;
  423.  
  424.         case WM_COMMAND:
  425.             switch(wParam)
  426.                 {
  427.                 case IDC_LISTBOX:
  428.                     switch(HIWORD(lParam))
  429.                         {
  430.                         case LBN_SETFOCUS:
  431.                             ListBoxFocus = TRUE;
  432.                             SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_SETCURSEL,
  433.                               GetListBoxIndex(hDlg), 0L);
  434.                             return TRUE;
  435.  
  436.                         case LBN_KILLFOCUS:
  437.                             ListBoxFocus = FALSE;
  438.                             SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_SETCURSEL,
  439.                               -1, 0L);
  440.                             return TRUE;
  441.  
  442.                         case LBN_DBLCLK:
  443. DoubleClick:                DlgDirSelect(hDlg, TempStr, IDC_LISTBOX);
  444.                             UpdateListBox(hDlg);
  445.                             return (TRUE);
  446.                             break;
  447.  
  448.                         } /* end IDC_LISTBOX switch */
  449.                     break;
  450.  
  451.                 case IDOK:
  452.                     if(ListBoxFocus)
  453.                         goto DoubleClick;
  454.  
  455.                     GetDlgItemText(hDlg, IDC_PATH, Directory, sizeof(Directory));
  456.                     WritePrivateProfileString(szAppName, "Icon_Path",
  457.                       Directory, ProgInit);
  458.                     EndDialog(hDlg,TRUE);
  459.                     MakeFileList();
  460.                     return (TRUE);
  461.                     break;
  462.  
  463.                 case IDCANCEL:
  464.                     EndDialog(hDlg,FALSE);
  465.                     return (TRUE);
  466.                     break;
  467.  
  468.                 default:
  469.                     return (FALSE);
  470.                 } /* end WM_COMMAND SWITCH */
  471.         default:
  472.             return (FALSE);
  473.         }
  474.     }
  475.  
  476. /*----------------------------------------------------------------------------*/
  477. /* FUNCTION: UpdateListBox(HWND hDlg)                                         */
  478. /*                                                                            */
  479. /* PURPOSE:  Helper function for dialog window proc                           */
  480. /*----------------------------------------------------------------------------*/
  481. void UpdateListBox(HWND hDlg)
  482.     {
  483.     SetDlgItemText(hDlg, IDC_PATH, TempStr);
  484.     DlgDirList(hDlg, TempStr, IDC_LISTBOX, IDC_PATH, 0xC010);
  485.     SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_SETCURSEL,
  486.       GetListBoxIndex(hDlg), 0L);
  487.     }
  488.  
  489. /*----------------------------------------------------------------------------*/
  490. /* FUNCTION: GetListBoxIndex(HWND hDlg)                                       */
  491. /*                                                                            */
  492. /* PURPOSE:  Helper function for dialog window proc                           */
  493. /*----------------------------------------------------------------------------*/
  494. WORD GetListBoxIndex(HWND hDlg)
  495.     {
  496.     WORD rtn;
  497.  
  498.     rtn = (WORD)SendDlgItemMessage(hDlg, IDC_LISTBOX, LB_GETCURSEL, 0, 0L);
  499.     if(rtn == LB_ERR)
  500.         return 0;
  501.     else
  502.         return rtn;
  503.     }
  504.  
  505. /*----------------------------------------------------------------------------*/
  506. /* FUNCTION: StartApplication()                                               */
  507. /*                                                                            */
  508. /* PURPOSE:  Called when application is started                               */
  509. /*----------------------------------------------------------------------------*/
  510. void StartApplication()
  511.     {
  512.     GetModuleFileName(hInst, ProgInit, sizeof(ProgInit));
  513.     strcpy(strchr(ProgInit, '.'), ".ini");
  514.     GetPrivateProfileString(szAppName, "Icon_Path", "c:", Directory,
  515.       sizeof(Directory), ProgInit);
  516.     MakeFileList();
  517.     }
  518.  
  519. /*----------------------------------------------------------------------------*/
  520. /* FUNCTION: Message(uchr *fmt_string, ...)                                   */
  521. /*                                                                            */
  522. /* PURPOSE:  Simplify calls to MessageBox()                                   */
  523. /*----------------------------------------------------------------------------*/
  524. void Message(uchr *fmt_string, ...)
  525.     {
  526.     uchr sbuf[256];
  527.  
  528.     va_list argptr;
  529.     va_start(argptr,fmt_string);
  530.     vsprintf(sbuf, fmt_string, argptr);
  531.     va_end(argptr);
  532.     MessageBox(ghWnd, sbuf, szAppTitle, MB_OK);
  533.     }
  534.  
  535. /*----------------------------------------------------------------------------*/
  536. /* FUNCTION: MakeFileList()                                                   */
  537. /*                                                                            */
  538. /* PURPOSE:  Build a list of all all icon file names in a directory           */
  539. /*----------------------------------------------------------------------------*/
  540. void MakeFileList()
  541.     {
  542.     char temp[80];
  543.  
  544.     sprintf(temp, "%s\\*.ico", Directory);
  545.     SendMessage(hWndList, LB_RESETCONTENT, 0, 0L);
  546.     SendMessage(hWndList, LB_DIR, 0, (LONG)(LPSTR)temp);
  547.  
  548.     FileCount    = (WORD)SendMessage(hWndList, LB_GETCOUNT, 0, 0L);
  549.     CurrentIndex = 0;
  550.     AdjustScrollBar();
  551.     InvalidateRect(ghWnd,NULL,TRUE);        /* force repaint                  */
  552.     }
  553.  
  554. /*----------------------------------------------------------------------------*/
  555. /* FUNCTION: AdjustScrollBar()                                                */
  556. /*                                                                            */
  557. /* PURPOSE:  Sets vertical scroll bar thumbtrack indicator                    */
  558. /*----------------------------------------------------------------------------*/
  559. void AdjustScrollBar()
  560.     {
  561.     if(IconsInWindow < FileCount)
  562.         {
  563.         SetScrollRange(ghWnd, SB_VERT, 0, FileCount, FALSE);
  564.         SetScrollPos(ghWnd, SB_VERT, (CurrentIndex > IconsInWindow ?
  565.           CurrentIndex+IconsInWindow : CurrentIndex), TRUE);
  566.         }
  567.     else
  568.         ShowScrollBar(ghWnd, SB_VERT, 0);   /* hide scroll bar                */
  569.     }
  570.  
  571. /*----------------------------------------------------------------------------*/
  572. /* FUNCTION: DisplayIconBitMap(HANDLE hBuf, uint xLoc, uint yLoc)             */
  573. /*                                                                            */
  574. /* PURPOSE:  Display 1 icon from a bitmap                                     */
  575. /*----------------------------------------------------------------------------*/
  576. void DisplayIconBitMap(HANDLE hBuf, uint xLoc, uint yLoc)
  577.     {
  578.     HDC              hDC;
  579.     LPSTR            pbuf;
  580.     LPICONDIRECTORY  lpid;
  581.     LPBITMAPINFO     lpbi;
  582.  
  583.     lpid = (LPICONDIRECTORY)GlobalLock(hBuf);
  584.     lpbi = (LPBITMAPINFO)(lpid + 1);
  585.  
  586.     pbuf = (LPSTR)lpbi + (int)(lpbi->bmiHeader.biSize) +
  587.            (lpid->ColorCount * sizeof(RGBQUAD));
  588.  
  589.     hDC = GetDC(ghWnd);
  590.     SetDIBitsToDevice(hDC, xLoc, yLoc,
  591.                       (WORD)lpid->Width,
  592.                       (WORD)lpid->Height,
  593.                       0, 0, 0,
  594.                       (WORD)lpid->Width,
  595.                       pbuf,
  596.                       (LPBITMAPINFO)lpbi,
  597.                       DIB_RGB_COLORS);
  598.     ReleaseDC(ghWnd, hDC);
  599.  
  600.     GlobalUnlock(hBuf);
  601.     GlobalFree(hBuf);
  602.     }
  603.  
  604. /*----------------------------------------------------------------------------*/
  605. /* FUNCTION: GetIconBitMap(LPSTR FileName)                                    */
  606. /*                                                                            */
  607. /* PURPOSE:  Get an icon bit map from an icon file                            */
  608. /*----------------------------------------------------------------------------*/
  609. HANDLE GetIconBitMap(LPSTR FileName)
  610.     {
  611.     int             IconFile;
  612.     DWORD           szDir = sizeof(ICONDIRECTORY);
  613.     HANDLE          hMem;
  614.     OFSTRUCT        info;
  615.     LPICONDIRECTORY lpbi;
  616.  
  617.     LONG retn;
  618.     int  retn1;
  619.  
  620.     if((IconFile = OpenFile(FileName, &info, OF_READ)) == -1)
  621.         return -1;
  622.  
  623.     _llseek(IconFile, sizeof(ICONFILEHDR), SEEK_SET);   /* seek to icon dir   */
  624.  
  625.     hMem = GlobalAlloc(GMEM_MOVEABLE, szDir);
  626.     if(!hMem)
  627.         {
  628.         Message("GlobalAlloc failed");
  629.         close(IconFile);
  630.         return -1;
  631.         }
  632.  
  633.     lpbi = (LPICONDIRECTORY)GlobalLock(hMem);
  634.     _lread(IconFile, (LPSTR)lpbi, (WORD)szDir);        /* read icon directory */
  635.  
  636.     hMem = GlobalReAlloc(hMem, lpbi->DIBSize + szDir, GMEM_MOVEABLE);
  637.     if(!hMem)
  638.         {
  639.         Message("GlobalReAlloc failed");
  640.         close(IconFile);
  641.         return -1;
  642.         }
  643.     else
  644.         {
  645.         retn = _llseek(IconFile, lpbi->DIBOffset, SEEK_SET);
  646.         retn1 = _lread(IconFile, (LPSTR)(lpbi+1), (WORD)lpbi->DIBSize);
  647.         GlobalUnlock(hMem);
  648.         }
  649.  
  650.      close(IconFile);
  651.      return hMem;
  652.      }
  653.  
  654. /*----------------------------------------------------------------------------*/
  655. /* FUNCTION: ViewIcons(HDC hDC)                                               */
  656. /*                                                                            */
  657. /* PURPOSE:  Display the max number of icons that will fit in current window  */
  658. /*----------------------------------------------------------------------------*/
  659. void ViewIcons(HDC hDC)
  660.     {
  661.     int row;
  662.  
  663.     if(FileCount == 0)
  664.         return;
  665.  
  666.     for(row = 0; row < yMaxIcons ;row++)
  667.         ViewIconRow(hDC, row);
  668.     }
  669.  
  670. /*----------------------------------------------------------------------------*/
  671. /* FUNCTION: ViewIconRow(HDC hDC, int row)                                    */
  672. /*                                                                            */
  673. /* PURPOSE:  Display 1 row of icons                                           */
  674. /*----------------------------------------------------------------------------*/
  675. void ViewIconRow(HDC hDC, int row)
  676.     {
  677.     int   i;
  678.     int   index;
  679.     int   xLoc = 4;
  680.     int   yLoc = 0;
  681.     char  Name[16];
  682.     char  FileName[80];
  683.     RECT  ClearRect;
  684.  
  685.     yLoc += (ySpacing * row);
  686.     index = (CurrentIndex + (row * xMaxIcons));
  687.  
  688.     ClearRect.left   = xLoc;
  689.     ClearRect.top    = yLoc;
  690.     ClearRect.right  = xLoc + (xSpacing * xMaxIcons);
  691.     ClearRect.bottom = yLoc + ySpacing;
  692.     FillRect(hDC, (LPRECT)&ClearRect, GetStockObject(WHITE_BRUSH));
  693.  
  694.     for(i = 0; (i < xMaxIcons) && (index < FileCount) ;i++, index++)
  695.         {
  696.         if(index >= 0)
  697.             {
  698.             SendMessage(hWndList, LB_GETTEXT, index, (LONG)(LPSTR)Name);
  699.             sprintf(FileName, "%s\\%s", Directory, Name);
  700.             ShowIcon(FileName, xLoc, yLoc);
  701.             *(strrchr(Name, '.')) = 0;
  702.             TextOut(hDC, xLoc, yLoc+39, Name, strlen(Name));
  703.             }
  704.         xLoc += xSpacing;
  705.         }
  706.     }
  707.  
  708. /*----------------------------------------------------------------------------*/
  709. /* FUNCTION: ShowIcon(uchr *FileName, uint xLoc, uint yLoc)                   */
  710. /*                                                                            */
  711. /* PURPOSE:  Display 1 icon from an icon file                                 */
  712. /*----------------------------------------------------------------------------*/
  713. void ShowIcon(uchr *FileName, uint xLoc, uint yLoc)
  714.     {
  715.     HANDLE hBuf;
  716.     if((hBuf = GetIconBitMap(FileName)) == -1)
  717.         Message("Error when opening file %s", FileName);
  718.     else
  719.         DisplayIconBitMap(hBuf, xLoc, yLoc+4);
  720.     }
  721.  
  722.  
  723.